package vn.edu.ptithcm.pim.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Transaction;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import vn.edu.ptithcm.pim.dom.ActivityLog;
import vn.edu.ptithcm.pim.query.ActivityLogQuery;

@SuppressWarnings("rawtypes")
public class ActivityLogDao extends HibernateDaoSupport {

	public void save(ActivityLog activityLog) {
		try {
			Transaction tx = getSession().beginTransaction();
			getHibernateTemplate().save(activityLog);
			tx.commit();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public void saveOrUpdate(ActivityLog activityLog) {
		try {
			Transaction tx = getSession().beginTransaction();
			getHibernateTemplate().saveOrUpdate(activityLog);
			tx.commit();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public void lock(ActivityLog activityLog) {
		try {
			Transaction tx = getSession().beginTransaction();
			getHibernateTemplate().lock(activityLog, LockMode.NONE);
			tx.commit();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public void delete(ActivityLog activityLog) {
		try {
			Transaction tx = getSession().beginTransaction();
			getHibernateTemplate().delete(activityLog);
			tx.commit();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public ActivityLog merge(ActivityLog activityLog) {
		try {
			return (ActivityLog) getHibernateTemplate().merge(activityLog);
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public ActivityLog findById(java.lang.Long id) {
		try {
			return (ActivityLog) getHibernateTemplate().get(ActivityLog.class,
					id);
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public List findByExample(ActivityLog activityLog) {
		try {
			return getHibernateTemplate().findByExample("ActivityLog",
					activityLog);
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public List getAll() {
		try {
			return getHibernateTemplate().loadAll(ActivityLog.class);
		} catch (RuntimeException re) {
			throw re;
		}
	}

	@SuppressWarnings("unchecked")
	public List<ActivityLog> findByQuery(Long projectId,
			ActivityLogQuery activityLogQuery, int max, int offset) {
		try {
			if ("Keyword...".equals(activityLogQuery.getSearchKey())) {
				activityLogQuery.setSearchKey("");
			}
			Criteria c = getSession().createCriteria(ActivityLog.class);
			c.add(Restrictions.eq("projectId", projectId));
			if (!activityLogQuery.getSearchKey().isEmpty()) {
				if ("Name".equals(activityLogQuery.getTitle())) {
					c.add(Restrictions.ilike("subjectName", "%"
							+ activityLogQuery.getSearchKey() + "%"));
				} else if ("Visa".equals(activityLogQuery.getTitle())) {
					c.add(Restrictions.ilike("createdBy", "%"
							+ activityLogQuery.getSearchKey() + "%"));
				} else {
					Criterion name = Restrictions.ilike("subjectName", "%"
							+ activityLogQuery.getSearchKey() + "%");
					Criterion visa = Restrictions.ilike("createdBy", "%"
							+ activityLogQuery.getSearchKey() + "%");
					c.add(Restrictions.or(name, visa));
				}
			}
			if (!activityLogQuery.getActionType().isEmpty()) {
				c.add(Restrictions.ilike("message",
						activityLogQuery.getActionType()));
			}
			if (!activityLogQuery.getItemType().isEmpty()) {
				c.add(Restrictions.ilike("subjectType",
						activityLogQuery.getItemType()));
			}
			if (activityLogQuery.getFrom() != null) {
				c.add(Restrictions.ge("createdAt", activityLogQuery.getFrom()));
			}
			if (activityLogQuery.getTo() != null) {
				c.add(Restrictions.le("createdAt", activityLogQuery.getTo()));
			}
			c.setFirstResult(offset).setMaxResults(max);
			c.addOrder(Order.desc("createdAt"));
			return c.list();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public int countByQuery(Long projectId, ActivityLogQuery activityLogQuery) {
		try {
			if ("Keyword...".equals(activityLogQuery.getSearchKey())) {
				activityLogQuery.setSearchKey("");
			}
			Criteria c = getSession().createCriteria(ActivityLog.class);
			c.add(Restrictions.eq("projectId", projectId));
			if (!activityLogQuery.getSearchKey().isEmpty()) {
				if ("Name".equals(activityLogQuery.getTitle())) {
					c.add(Restrictions.ilike("subjectName", "%"
							+ activityLogQuery.getSearchKey() + "%"));
				} else if ("Visa".equals(activityLogQuery.getTitle())) {
					c.add(Restrictions.ilike("createdBy", "%"
							+ activityLogQuery.getSearchKey() + "%"));
				} else {
					Criterion name = Restrictions.ilike("subjectName", "%"
							+ activityLogQuery.getSearchKey() + "%");
					Criterion visa = Restrictions.ilike("createdBy", "%"
							+ activityLogQuery.getSearchKey() + "%");
					c.add(Restrictions.or(name, visa));
				}
			}
			if (!activityLogQuery.getActionType().isEmpty()) {
				c.add(Restrictions.ilike("message",
						activityLogQuery.getActionType()));
			}
			if (!activityLogQuery.getItemType().isEmpty()) {
				c.add(Restrictions.ilike("subjectType",
						activityLogQuery.getItemType()));
			}
			if (activityLogQuery.getFrom() != null) {
				c.add(Restrictions.ge("createdAt", activityLogQuery.getFrom()));
			}
			if (activityLogQuery.getTo() != null) {
				c.add(Restrictions.le("createdAt", activityLogQuery.getTo()));
			}
			c.setProjection(Projections.rowCount());
			return Integer.valueOf(c.list().get(0).toString());
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public void deleteById(java.lang.Long id) {
		try {
			Transaction tx = getSession().beginTransaction();
			StringBuilder sql = new StringBuilder();
			sql.append(
					"delete ActivityLog as activityLog where activityLog.activityLogId ='")
					.append(id).append("'");
			getHibernateTemplate().bulkUpdate(sql.toString());
			tx.commit();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public void deleteByProjectId(java.lang.Long id) {
		try {
			Transaction tx = getSession().beginTransaction();
			StringBuilder sql = new StringBuilder();
			sql.append(
					"delete ActivityLog as activityLog where activityLog.projectId ='")
					.append(id).append("'");
			getHibernateTemplate().bulkUpdate(sql.toString());
			tx.commit();
		} catch (RuntimeException re) {
			throw re;
		}
	}
}
